﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CUREEntity;
using CUREBusinessLogicLayer;
using System.Data;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.SS.UserModel;
using System.Reflection;
using System.Collections;
using CUREEntity.SystemManagement;


namespace CURE.Common
{
    public class Commonfun
    {
        //#region   日志参数
        //string ID = Convert.ToDecimal(context.Request.Cookies["MWS_User"]["ID"]).ToString();
        public string yymmss = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.ToString("HHmmss");
        //string mess = code;
        //string masterid = "";
        //#endregion
    
        #region   日志中添加信息
        public void AddEventLog(string yymmss,string message,string masterid)
        {
            EventLogModel eventlog = new EventLogModel();          
            eventlog.Yyyymmddhhmmss = yymmss;
            eventlog.Message = message;
            eventlog.Masterid = masterid;
            EventLogBll eventbll = new EventLogBll();
            eventbll.AddEventLogin(eventlog);
        }
        #endregion

        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 导入数据时导入数据重复
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string failure1(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"重复条码!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string successs(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"navTabId\":\"" + navTabId + "\"}";
        }

        #endregion

        #region 时间类型转换
        /// <summary>
        /// DateTime转换为数据库格式字符串
        /// </summary>
        /// <param name="datetime">时间对象</param>
        /// <returns>数据库存储格式的字符串</returns>
        public string DateTimeToDB(DateTime datetime)
        {
            return datetime.ToString("yyyyMMddHHmmss");
        }
    
        /// <summary>
        /// 数据库中时间数据转换成显示格式的字符串
        /// </summary>
        /// <param name="datetime">数据库中的时间数据</param>
        /// <returns>时间显示格式字符串</returns>
        public string DateTimeFromDB(object datetime)
        {
            if (datetime == null || datetime.ToString() == "")
            {
                return "";
            }
            string datetimestr = datetime.ToString();
            datetimestr = datetimestr.Substring(0, 8);
            datetimestr = datetimestr.Insert(6, "-");
            datetimestr = datetimestr.Insert(4, "-");
            return datetimestr;
        }

        /// <summary>
        /// DateTime转换为显示用的字符串类型
        /// </summary>
        /// <param name="datetime">时间对象</param>
        /// <returns>时间显示格式字符串</returns>
        public string DateTimeToUI(DateTime datetime)
        {
            return datetime.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 数据库中字符串时间数据转换成显示格式的字符串(如：2014-03-20 10:20:30)
        /// </summary>
        /// <param name="datetime">数据库中的时间数据</param>
        /// <returns>时间显示格式字符串</returns>
        public string StringToDatetime(object datetime)
        {
            if (datetime == null || datetime.ToString()=="")
            {
                return "";
            }

            string datetimestr = datetime.ToString();
            //datetimestr = datetimestr.Substring(0, 14);
            datetimestr = datetimestr.Substring(0, 12);
            //datetimestr = datetimestr.Insert(12, ":");
            datetimestr = datetimestr.Insert(10, ":");
            datetimestr = datetimestr.Insert(8, " ");
            datetimestr = datetimestr.Insert(6, "-");
            datetimestr = datetimestr.Insert(4, "-");
            return datetimestr;
        }

        #endregion

        #region Excel导入
        /// <summary>读取excel
        /// 默认第一行为标头
        /// </summary>
        /// <param name="strFileName">excel文档路径</param>
        /// <returns></returns>
        public DataTable Import(string strFileName)
        {
            DataTable dt = new DataTable();

            HSSFWorkbook hssfworkbook;
            using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
            {
                hssfworkbook = new HSSFWorkbook(file);
            }
            ISheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

            IRow headerRow = sheet.GetRow(0);
            int cellCount = headerRow.LastCellNum;

            for (int j = 0; j < cellCount; j++)
            {
                ICell cell = headerRow.GetCell(j);
                dt.Columns.Add(cell.ToString());
            }

            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)//跳过表头
            {
                IRow row = sheet.GetRow(i);
                DataRow dataRow = dt.NewRow();

                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) != null)
                        dataRow[j] = row.GetCell(j).ToString();
                }

                dt.Rows.Add(dataRow);
            }
            return dt;
        } 
        #endregion
        /// <summary>
        /// 从第二行开始读取csv文件的第一列
        /// </summary>
        /// <param name="filePathName">文件路径</param>
        /// <returns></returns>
        public List<string> ReadCSV(string filePathName)
        {
            List<String[]> ls = new List<String[]>();
            List<string> tracklt = new List<string>();
            StreamReader fileReader = new StreamReader(filePathName);
            string strLine = "";
            while (strLine != null)
            {
                strLine = fileReader.ReadLine();
                if (strLine != null && strLine.Length > 0)
                {
                    ls.Add(strLine.Split(','));
                }
            }
            fileReader.Close();
            for (int i = 1; i < ls.Count; i++)
            {
                tracklt.Add(ls[i][0]);
            }
            return tracklt;
        }

        /// <summary>
        /// 完成修改操作返回的url
        /// </summary>
        /// <param name="context"></param>
        /// <param name="lastUrl">根目录下的页面文件夹</param>
        /// <returns></returns>
        public string ReturnUrl(HttpContext context,string lastUrl)
        {
            int index = context.Request.UrlReferrer.Fragment.IndexOf("/");
            return lastUrl + context.Request.UrlReferrer.Fragment.Substring(index, context.Request.UrlReferrer.Fragment.Length - index);
        }

    }
}